package com.heatup.apt.api.controller;

import com.heatup.apt.api.service.StoreService;
import com.heatup.apt.common.constants.ErrorCodeConstants;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.api.store.StoreResultModel;
import com.heatup.apt.model.store.StoreInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by b3st9u on 16/8/20.
 */

@RequestMapping(value = "/store")
@Controller
public class StoreController extends BaseController {
    protected static final Logger log = LoggerFactory.getLogger(StoreController.class);

    @Autowired
    private StoreService storeService;


    /**
     * API 门店详情接口
     *
     * @param id 门店 ID
     * @param callback 前端回调
     * @author blank 2017/8/16 下午2:41
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = {RequestMethod.POST, RequestMethod.GET})
    private String getStoreResult(Integer id, String callback) {
//        int storeId = RequestTools.RequestInt(request, "id", 0);
//        String callback = RequestTools.RequestString(request, "callback", "");
        ApiResultModel<StoreResultModel> apiResultModel = new ApiResultModel<>();
        StoreResultModel storeModel;
        try {

            storeModel = storeService.queryStoreByStoreId(id);
            if (storeModel == null || storeModel.getId() == null) {
                apiResultModel.setMsg(ErrorCodeConstants.Code.STORE_NOT_FIND.getMessage());
            }
            apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
            apiResultModel.setData(storeModel);
            apiResultModel.setMsg("");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(ErrorCodeConstants.Code.STORE_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause());
            apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
            apiResultModel.setRet(ApiResultModel.RET_ERROR);
            apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage() + e.getMessage() + e.getCause());
        }
        apiResultModel.setLogined(isLogined());
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
    }

    /**
     * 进店模式新 add by blank
     *
     * @param callback
     * @return
     */
    @RequestMapping(value = "/enterStore", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    private String enterStoreResource(@RequestParam(defaultValue = "") String callback) {
        ApiResultModel<Map<String, Object>> apiResultModel = new ApiResultModel<>();
        Map<String, Object> resultMap = new HashMap();
        apiResultModel.setLogined(isLogined());
        try {
            resultMap = storeService.enterStoreResourceList();
            apiResultModel.setData(resultMap);
            apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
            apiResultModel.setMsg("");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(ErrorCodeConstants.Code.STORE_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause());
            apiResultModel.setErrorCode(ErrorCodeConstants.Code.STORE_ERROR_EXCEPTION.getCode());
            apiResultModel.setMsg(ErrorCodeConstants.Code.STORE_ERROR_EXCEPTION.getMessage() + e.getMessage() + e.getCause());
            apiResultModel.setRet(ApiResultModel.RET_ERROR);
        }
        apiResultModel.setLogined(isLogined());
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
    }

    /**
     * 门店列表
     *
     * @param longitudeLatitude
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
    private String getStoreList(@RequestParam(required = false) Integer pageIndex,
                                @RequestParam(required = false) Integer pageSize,
                                String longitudeLatitude,@RequestParam(required = false)String cityId,@RequestParam(required = false)String districtId,
                                String callback) {
        if(pageIndex ==null)
            pageIndex = 1;
        if(pageSize == null)
            pageSize =10;
        if (pageIndex > 0)
            --pageIndex;
        if (pageSize <=0)
            pageSize = 10;

        ApiResultModel apiResultModel = new ApiResultModel<>();
        try {
            apiResultModel = storeService.queryAllStore(pageIndex, pageSize, longitudeLatitude,cityId,districtId, this.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(ErrorCodeConstants.Code.STORE_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause());
            apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
            apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage() + e.getMessage() + e.getCause());
            apiResultModel.setRet(ApiResultModel.RET_ERROR);
        }

        apiResultModel.setLogined(isLogined());
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
    }


    /**
     * 门店列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/all_store", method = {RequestMethod.POST, RequestMethod.GET})
    private String getAllStore(HttpServletRequest request) {
        ApiResultModel<List<StoreInfo>> apiResultModel = new ApiResultModel<>(isLogined());
        String callback = RequestTools.RequestString(request, "callback", "");
        String courseId = RequestTools.RequestString(request, "courseId", "");
        apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
        apiResultModel.setLogined(isLogined());
        if("".equals(courseId)) {
        	 log.error("课程id为空");
             apiResultModel.setMsg("没找到对应的课程");
             apiResultModel.setRet(ApiResultModel.RET_ERROR);
        }
        List<StoreInfo> list = storeService.queryStoreListByCourseId(Integer.parseInt(courseId));
        apiResultModel.setData(list);
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
    }
}
